package com.mc.union;

public class UnionFind {
	
	private int[] id;
	public UnionFind(int n){
		id = new int[n];
		for(int i=0;i<n;i++){
			id[i] = i;
		}
	}
	public void union(int p,int q){
		if(connected(p,q))
			return;
		for(int i=0;i<id.length;i++){
			if(id[i]==id[q])id[i]=id[p];
		}
	}
	public int find(int p){
		return id[p];
	}
	public boolean connected(int p,int q){
		return find(p)==find(q);
	}
}
